package com.hongxun.modules.bss.controller;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hongxun.modules.api.entity.BMaterialEntity;
import com.hongxun.modules.api.service.BMaterialService;
import com.hongxun.modules.bss.entity.DMaterialModelEntity;
import com.hongxun.modules.bss.service.DMaterialModelService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hongxun.modules.bss.entity.DMaterialModelItemEntity;
import com.hongxun.modules.bss.service.DMaterialModelItemService;
import com.hongxun.common.utils.PageUtils;
import com.hongxun.common.utils.Query;
import com.hongxun.common.utils.R;




/**
 * 	模块材料数量
 * 
 * @author zkcn
 * @email youeremail@163.com
 * @date 2018-11-02 15:13:21
 */
@RestController
@RequestMapping("/bss/dmaterialmodelitem")
public class DMaterialModelItemController {
	@Autowired
	private DMaterialModelItemService dMaterialModelItemService;
	@Autowired
	private DMaterialModelService dMaterialModelService;
	@Autowired
	private BMaterialService bMaterialService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
//	@RequiresPermissions("bss:dmaterialmodelitem:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
        String wheres =" 1=1 ";
		if(query.get("modelGuid")!=null&&!query.get("modelGuid").toString().equals(""))
		{
			wheres+=" and  model_guid= '" + query.get("modelGuid").toString()+"' ";
		}
        query.put("filterSql",wheres+" ");
		List<DMaterialModelItemEntity> dMaterialModelItemList = dMaterialModelItemService.queryList(query);

		//	2019-9-16 15:15:45
		if(dMaterialModelItemList.size()>0){
			for (DMaterialModelItemEntity dMaterialModelItemEntity : dMaterialModelItemList) {
//				DMaterialModelEntity dme =  dMaterialModelService.queryObject(dMaterialModelItemEntity.getModelGuid());
				BMaterialEntity be =  bMaterialService.queryObject(dMaterialModelItemEntity.getMaterialGuid());
				if(be!=null) {
					dMaterialModelItemEntity.setModelGuid(be.getMName());
					dMaterialModelItemEntity.setMaterialGuid(be.getMParameter());
					dMaterialModelItemEntity.setDatastate(be.getMBigtype());
				}
			}
		}


		int total = dMaterialModelItemService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(dMaterialModelItemList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{guid}")
	@RequiresPermissions("bss:dmaterialmodelitem:info")
	public R info(@PathVariable("guid") String guid){
		DMaterialModelItemEntity dMaterialModelItem = dMaterialModelItemService.queryObject(guid);
		
		return R.ok().put("dMaterialModelItem", dMaterialModelItem);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
//	@RequiresPermissions("bss:dmaterialmodelitem:save")
	public R save(@RequestBody DMaterialModelItemEntity dMaterialModelItem){

		Map<String , Object> map = new HashMap<>();
		String wheres =" 1=1 ";
		map.put("filterSql",wheres+" ");
		List<DMaterialModelItemEntity> dMaterialModelItemList = dMaterialModelItemService.queryList(map);
		for (DMaterialModelItemEntity dMaterialModelItemEntity : dMaterialModelItemList) {
			if(dMaterialModelItem.getMaterialGuid().equals(dMaterialModelItemEntity.getMaterialGuid())
					&& dMaterialModelItem.getModelGuid().equals(dMaterialModelItemEntity.getModelGuid())){
				dMaterialModelItemEntity.setCount(dMaterialModelItem.getCount().add(dMaterialModelItemEntity.getCount()));
				dMaterialModelItemService.update(dMaterialModelItemEntity);
				return R.ok("该模块已添加相同材料，数量将进行累计");
			}
		}


		dMaterialModelItem.setDatastate("新增");
		dMaterialModelItemService.save(dMaterialModelItem);
		
		return R.ok("添加成功");
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("bss:dmaterialmodelitem:update")
	public R update(@RequestBody DMaterialModelItemEntity dMaterialModelItem){
		dMaterialModelItemService.update(dMaterialModelItem);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
//	@RequiresPermissions("bss:dmaterialmodelitem:delete")
	public R delete(@RequestBody String[] guids){
		dMaterialModelItemService.deleteBatch(guids);
		
		return R.ok();
	}


	/**
	 * 添加材料数量
	 * @Time  2019-9-17 17:34:43
	 * @param dMaterialModelItem
	 * @return
	 */
	@RequestMapping("/saveMaterial")
	public R saveMaterial(@RequestBody DMaterialModelItemEntity dMaterialModelItem){

		Map<String , Object> map = new HashMap<>();
		String wheres =" 1=1 ";
		map.put("filterSql",wheres+" ");
		List<DMaterialModelItemEntity> dMaterialModelItemList = dMaterialModelItemService.queryList(map);
		if(dMaterialModelItemList.size()>0){
			for (DMaterialModelItemEntity dMaterialModelItemEntity : dMaterialModelItemList) {
				if(dMaterialModelItem.getMaterialGuid().equals(dMaterialModelItemEntity.getMaterialGuid())
						&& dMaterialModelItem.getModelGuid().equals(dMaterialModelItemEntity.getModelGuid())){
					dMaterialModelItemEntity.setCount(dMaterialModelItem.getCount().add(dMaterialModelItemEntity.getCount()));
					dMaterialModelItemService.update(dMaterialModelItemEntity);
					return R.ok("该模块已添加相同材料，数量将进行累计");
				}
			}
		}


		dMaterialModelItem.setDatastate("新增");
		dMaterialModelItemService.save(dMaterialModelItem);

		return R.ok("添加成功");
	}


	/**
	 * 	修改数量
	 * 	@Time 2019-9-18 09:36:25
	 * @param
	 * @return
	 */
	@RequestMapping("/updCount")
	public R updCount(@RequestParam Map<String, Object> Parms){
		DMaterialModelItemEntity dmie = new DMaterialModelItemEntity();
		dmie.setGuid(Parms.get("guid").toString());
		dmie.setCount(BigDecimal.valueOf(Double.parseDouble(Parms.get("count").toString())));
		dmie.setPaixu(BigDecimal.valueOf(Double.parseDouble(Parms.get("paixu").toString())));

		dmie.setDatastate("修改");
		dMaterialModelItemService.update(dmie);

		return R.ok();
	}




}
